//
//  PEDetailsViewController.m
//  YouWen
//
//  Created by liqiang－pc on 15/3/10.
//  Copyright (c) 2015年 刘永和. All rights reserved.
//

#import "PEDetailsViewController.h"

@interface PEDetailsViewController ()

@end

@implementation PEDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //创建headDeailsScrollView视图
    
    self.headDeailsScrollView = [[PEDetailsScrollView alloc] initWithFrame:[[UIScreen mainScreen]bounds]headLine:self.headLine];
    self.headDeailsScrollView.backgroundColor = [NightModeHandle shareNightModeHandle].allViewColor;
    
    [self.view addSubview:self.headDeailsScrollView];
    
    
    UIImage *backImage = [UIImage  imageNamed:@"返回(1).png"];
    backImage = [backImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage: backImage style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonAction:)];
    UIImage *collect = [UIImage imageNamed:@"star@2x.png"];
    self.collectbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.collectbutton setBackgroundImage:collect forState:UIControlStateNormal];
    self.collectbutton.frame = CGRectMake(0, 0, 30, 30);
    [self.collectbutton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.collectbutton] ;
    
    
    UIImage *collect1 = [UIImage imageNamed:@"star_selected@2x.png"];
    self.collectbutton1 = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.collectbutton1 setBackgroundImage:collect1 forState:UIControlStateNormal];
    self.collectbutton1.frame = CGRectMake(0, 0, 30, 30);
    [self.collectbutton1 addTarget:self action:@selector(button1Action:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    [[HeadLineDataHandle shareHeadLineDataHandle] openDB];
    [[HeadLineDataHandle shareHeadLineDataHandle] createTable];
    
    for (HeadLine *headLine in [[HeadLineDataHandle shareHeadLineDataHandle]selectHead]) {
        if ([headLine.title isEqualToString:self.headLine.title]) {
            self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:self.collectbutton1];
            return;
        }else{
            
            self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:self.collectbutton];
        }
    }
    
    
}


-(void)leftButtonAction:(UIButton *)button
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)buttonAction:(UIButton *)button
{
    
    [[HeadLineDataHandle shareHeadLineDataHandle] openDB];
    [[HeadLineDataHandle shareHeadLineDataHandle] createTable];
    
    for (HeadLine *headLine in [[HeadLineDataHandle shareHeadLineDataHandle]selectHead]) {
        if ([headLine.title isEqualToString:self.headLine.title]) {
            NSLog(@"已经收藏");
            return;
        }
    }
    [[HeadLineDataHandle shareHeadLineDataHandle]insertHeadLine:self.headLine];
    NSLog(@"收藏成功!");
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:self.collectbutton1];
    
    self.alertView = [[UIAlertView alloc]initWithTitle:@"收藏成功" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
    [self.view addSubview:self.alertView];
    [self.alertView show];
    [NSTimer scheduledTimerWithTimeInterval:0.5f target:self selector:@selector(timerAction) userInfo:nil repeats:NO];
    
    
    
}

- (void)timerAction
{
    [self.alertView dismissWithClickedButtonIndex:0 animated:YES];
    [self.alertView2 dismissWithClickedButtonIndex:0 animated:YES];
}



- (void)button1Action:(UIButton *)button
{
    [[HeadLineDataHandle shareHeadLineDataHandle]openDB];
    [[HeadLineDataHandle shareHeadLineDataHandle]createTable];
    [[HeadLineDataHandle shareHeadLineDataHandle]deleteHeadLine:self.headLine];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:self.collectbutton];
    self.alertView2 = [[UIAlertView alloc]initWithTitle:@"取消收藏" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
    [self.view addSubview:self.alertView2];
    [self.alertView2 show];
    
    [NSTimer scheduledTimerWithTimeInterval:0.5f target:self selector:@selector(timerAction) userInfo:nil repeats:NO];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
